using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using BitMango.Diagnostics;

namespace BitMango
{
	public static class Encryptor
	{
		public static string Encrypt(string s)
		{
			return Encryptor.Encrypt(s, Encryptor.rijnKey, Encryptor.rijnIV);
		}

		public static string Decrypt(string s)
		{
			return Encryptor.Decrypt(s, Encryptor.rijnKey, Encryptor.rijnIV);
		}

		private static string Encrypt(string s, byte[] key, byte[] IV)
		{
			RijndaelManaged rijndaelManaged = new RijndaelManaged();
			rijndaelManaged.Mode = CipherMode.CBC;
			rijndaelManaged.Padding = PaddingMode.Zeros;
			string result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor(key, IV))
				{
					using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
					{
						using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
						{
							streamWriter.Write(Encryptor.PKCS7_Pad(s));
						}
					}
				}
				result = Convert.ToBase64String(memoryStream.ToArray());
			}
			rijndaelManaged.Clear();
			return result;
		}

		private static string Decrypt(string s, byte[] key, byte[] IV)
		{
			string result = null;
			RijndaelManaged rijndaelManaged = new RijndaelManaged();
			rijndaelManaged.Mode = CipherMode.CBC;
			rijndaelManaged.Padding = PaddingMode.Zeros;
			using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(s)))
			{
				using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor(key, IV))
				{
					using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read))
					{
						using (StreamReader streamReader = new StreamReader(cryptoStream))
						{
							List<char> list = new List<char>();
							int num;
							do
							{
								num = streamReader.ReadBlock(Encryptor.READ_BUFFER, 0, 1024);
								if (num == 1024)
								{
									list.AddRange(Encryptor.READ_BUFFER);
								}
								else
								{
									list.AddRange(Encryptor.READ_BUFFER.Take(num).ToArray<char>());
								}
							}
							while (num == 1024);
							result = Encryptor.PKCS7_UnPad(list.ToArray());
						}
					}
				}
			}
			rijndaelManaged.Clear();
			return result;
		}

		private static char[] PKCS7_Pad(string val)
		{
			List<char> list = new List<char>(val);
			int num = 16 - val.Length % 16;
			char[] array = new char[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = (char)num;
			}
			list.AddRange(array);
			return list.ToArray();
		}

		private static string PKCS7_UnPad(char[] val)
		{
			Debugger.Assert(val.Length % 16 == 0);
			List<char> list = new List<char>(val);
			int num = Convert.ToInt32(list[list.Count - 1]);
			return new string(list.GetRange(0, list.Count - num).ToArray());
		}

		public static string EncodeWithKey(string text, string key)
		{
			byte[] array = new byte[text.Length];
			for (int i = 0; i < text.Length; i++)
			{
				array[i] = (byte)(text[i] ^ key[i % key.Length]);
			}
			return Convert.ToBase64String(array, 0, array.Length);
		}

		private static byte[] rijnKey = Encoding.UTF8.GetBytes("cafe2080cafe2080");

		private static byte[] rijnIV = Encoding.UTF8.GetBytes("cafe2080cafe2080");

		private const int READ_BUFFER_SIZE = 1024;

		private static char[] READ_BUFFER = new char[1024];
	}
}
